package com.hzjy.api.controller;

import com.hzjy.api.service.impl.BqUserServiceImpl;
import com.hzjy.api.service.impl.SmsServiceImpl;
import com.hzjy.api.vo.BqUser;
import com.hzjy.common.core.domain.AjaxResult;
import com.hzjy.common.utils.CharUtil;
import com.hzjy.common.utils.PageData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 短信接口
 *
 * @author SS
 */
@Api("短信接口")
@RestController
@RequestMapping("/api/sms")
public class SmsController {
    private static final Logger log = LoggerFactory.getLogger(SmsController.class);
    @Autowired
    private SmsServiceImpl smsService;
    @Autowired
    private BqUserServiceImpl bqUserService;

    @ApiOperation("发送短信")
    @PostMapping("/sendOutSms")
    public AjaxResult sendOutSms(@RequestBody PageData pd) {
        String phone = pd.get("phone").toString();
        String type = pd.get("type").toString();
        BqUser bqUser = bqUserService.findByPhone(phone);
        if (Integer.parseInt(type) == 0) {
            log.info("注册发送验证码..." + phone);
            if(bqUser != null){
                return AjaxResult.error(500, "该手机号码已注册,请直接登录.");
            }
        } else if (Integer.parseInt(type) == 1) {
            log.info("登录发送验证码前校验用户是否注册..." + phone);
            if (bqUser == null) {
                return AjaxResult.error(500, "该手机号码未注册,请先注册.");
            }
        }
        String code = CharUtil.getRandomNum(6); //获得随机的6位数字
        smsService.createSignature(phone, code);
        return AjaxResult.success("短信已发送");
    }
}
